package com.xz.courseschedule.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xz.courseschedule.entity.Timetable;
import com.xz.courseschedule.service.TimetableService;
import com.xz.courseschedule.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课表管理控制器
 * @Author: zys
 * @CreateTime: 2025-05-25
 */
@Tag(name = "课表管理", description = "课表信息的管理、查询和冲突检查")
@RestController
@RequestMapping("/timetable")
public class TimetableController {

    @Autowired
    private TimetableService timetableService;

    @Operation(summary = "获取课表列表", description = "获取系统中所有课表信息")
    @GetMapping("/getList")
    public Result getList() {
        return timetableService.getList();
    }

    /*****************************手动排课的冲突检查*************************************/

    @Operation(summary = "获取冲突的列名列表", description = "获取指定学期课表中存在冲突的班级/教师/教室编号列表")
    @GetMapping("/getConflictingColumnNameList/{semesterId}/{columnName}")
    public Result getConflictingColumnNameList(
            @Parameter(description = "学期ID") @PathVariable Long semesterId,
            @Parameter(description = "列名类型，可选值：class_no/teacher_no/room_no") @PathVariable String columnName) {
        return timetableService.getConflictingColumnNameList(semesterId, columnName);
    }

    @Operation(summary = "获取冲突的时间段ID列表", description = "获取指定学期课表中特定编号存在冲突的时间段ID列表")
    @GetMapping("/getConflictingTimeslotIdList/{semesterId}/{columnName}/{no}")
    public Result getConflictingTimeslotIdList(
            @Parameter(description = "学期ID") @PathVariable Long semesterId,
            @Parameter(description = "列名类型，可选值：class_no/teacher_no/room_no") @PathVariable String columnName,
            @Parameter(description = "具体编号") @PathVariable String no) {
        return timetableService.getConflictingTimeslotIdList(semesterId, columnName, no);
    }

    @Operation(summary = "获取冲突列表", description = "获取与指定课表记录存在冲突的其他课表记录及冲突类型")
    @GetMapping("/getConfilctList/{id}")
    public Result getConfilctList(@Parameter(description = "课表记录ID") @PathVariable Long id) {
        return timetableService.getConfilctList(id);
    }

    @Operation(summary = "预演时间段调整", description = "获取指定课表记录调整到其他时间段后无冲突的可选时间段列表")
    @GetMapping("/rehearsalChangeTimeslot/{id}")
    public Result rehearsalChangeTimeslot(@Parameter(description = "课表记录ID") @PathVariable Long id) {
        return timetableService.rehearsalChangeTimeslot(id);
    }

    /*****************************update*************************************/
    @Operation(summary = "手动调课", description = "将指定课表记录调整到新的时间段")
    @PutMapping("/adjust/{srcId}/{destTimeslot}")
    public Result adjust(
            @Parameter(description = "源课表记录ID") @PathVariable Long srcId, 
            @Parameter(description = "目标时间段") @PathVariable Integer destTimeslot) {
        if (srcId == null || destTimeslot == null || destTimeslot == -1) {
            return Result.error(400,"手动调课参数有误");
        }
        return timetableService.adjust(srcId, destTimeslot);
    }

    /*****************************getXxx getXxxList*************************************/
    @Operation(summary = "根据班级编号查询课表", description = "获取指定班级的课表信息")
    @GetMapping("/{classNo}")
    public Result getTimetable(@Parameter(description = "班级编号") @PathVariable String classNo) {
        return timetableService.getTimetableByClassNo(classNo);
    }

    @Operation(summary = "根据学期和班级查询课表", description = "获取指定学期和班级的详细课表信息")
    @GetMapping("/class/{semesterId}/{classNo}")
    public Result getClassTimetable(
            @Parameter(description = "学期ID") @PathVariable("semesterId") Long semesterId,
            @Parameter(description = "班级编号") @PathVariable("classNo") String classNo) {
        LambdaQueryWrapper<Timetable> wrapper = new LambdaQueryWrapper<Timetable>()
                .eq(Timetable::getSemesterId, semesterId)
                .eq(Timetable::getClassNo, classNo)
                .orderByAsc(Timetable::getTimeslot);
        List<Timetable> timetableList = timetableService.list(wrapper);
        if (null == timetableList || timetableList.isEmpty()) {
            return Result.error(400,"该班级在该学期没有课表");
        }
        return timetableService.queryTimetableByClassNo(timetableList);
    }

    /**
     * 根据教室编号查找课表
     */
    @GetMapping("/room/{semesterId}/{roomNo}")
    public Result getRoomTimetable(@PathVariable("semesterId") Long semesterId,
                                   @PathVariable("roomNo") String roomNo) {
        LambdaQueryWrapper<Timetable> wrapper = new LambdaQueryWrapper<Timetable>()
                .eq(Timetable::getSemesterId, semesterId)
                .eq(Timetable::getRoomNo, roomNo)
                .orderByAsc(Timetable::getTimeslot);
        List<Timetable> timetableList = timetableService.list(wrapper);
        if (null == timetableList || timetableList.isEmpty()) {
            return Result.error(400,"该教室在该学期没有课表");
        }
        return timetableService.queryTimetableByClassNo(timetableList);
    }

    /**
     * 根据教师编号查找课表
     */
    @GetMapping("/teacher/{semesterId}/{teacherNo}")
    public Result getTeacherTimetable(@PathVariable("semesterId") Long semesterId,
                                      @PathVariable("teacherNo") String teacherNo) {
        LambdaQueryWrapper<Timetable> wrapper = new LambdaQueryWrapper<Timetable>()
                .eq(Timetable::getSemesterId, semesterId)
                .eq(Timetable::getTeacherNo, teacherNo)
                .orderByAsc(Timetable::getTimeslot);
        List<Timetable> timetableList = timetableService.list(wrapper);
        if (null == timetableList || timetableList.isEmpty()) {
            return Result.error(400,"该教师在该学期没有课表");
        }
        return timetableService.queryTimetableByClassNo(timetableList);
    }

}
